package com.ihome.android.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.content.Context;
import android.os.SystemClock;

public class TimeUtil {
	/**
	 * 
	 * @param host
	 *            time.foo.com
	 * @param timeout
	 *            socket timeout time like 3000(ms)
	 * @return
	 */
	public static long getNTPTimeByMills(String host, int timeout) {
		SntpClient client = new SntpClient();
		if (client.requestTime(host, timeout)) {
			long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
			return now;
		}
		else {
			return -1;
		}
	}

	public static void setSysTime(Context ctx, long timeMillies) {
		try {
			SystemClock.setCurrentTimeMillis(timeMillies);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setSysTimeZone(Context ctx, String timeZoneID) {
		if (timeZoneID != null) {
			AlarmManager a = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
			try {
				a.setTimeZone(timeZoneID);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static String getEventDate(String date_time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String datestr = "";
		Date date = null;
		try {
			date = format.parse(date_time);
			datestr = format.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return datestr;
	}

	public static String getEventTime(String date_time) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

		String timestr = "";
		Date date = null;
		try {
			date = format.parse(date_time);
			timestr = format.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timestr;
	}

	@SuppressLint("SimpleDateFormat")
	public static String getCurrentTime() {
		String strdate = null;
		Date date = new Date();
		if (date != null) {
			//SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmss");
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			strdate = format.format(date);
		}
		return strdate;
	}
	
	public static String getCurrentTimeAndWeekday() {
		long longtime = System.currentTimeMillis();
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(longtime);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DAY_OF_MONTH);
		int Month = mMonth + 1;
		String time = Month + "月" + mDay + "日," + getWeekOfDate();

		return time;
	}

	public static String getWeekOfDate() {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		Date curDate = new Date(System.currentTimeMillis());
		cal.setTime(curDate);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	@SuppressLint("SimpleDateFormat")
	public static boolean checkTimeValid(String time) {
		if (time == null)
			return false;

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmss");
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}

		if (date == null)
			return false;

		Date current = new Date();
		if (date.compareTo(current) > 0)
			return true;
		else
			return false;
	}

	@SuppressLint("SimpleDateFormat")
	public static long getTimeMillisFromString(String time) {
		if (time == null)
			return 0;

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmss");
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

		if (date == null)
			return 0;

		return date.getTime();
	}

	@SuppressLint("SimpleDateFormat")
	public static long getDelayTime(String time) {
		if (time == null)
			return 0;

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmss");
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

		if (date == null)
			return 0;

		return date.getTime() - (new Date()).getTime();
	}
	
	@SuppressLint("SimpleDateFormat")
	public static String getDefDate() {
		Date dt = new Date();
		// 最后的aa表示“上午”或“下午” HH表示24小时制 如果换成hh表示12小时制
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(dt);
	}
	
	@SuppressLint("SimpleDateFormat")
	public static String getDefDate1() {
		Date dt = new Date();
		// 最后的aa表示“上午”或“下午” HH表示24小时制 如果换成hh表示12小时制
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(dt);
	}
	
	@SuppressLint("SimpleDateFormat")
	public static long getDurationTime(String time) {
		if (time == null)
			return 0;
		
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddkkmmss");         
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
		
		if (date == null)
			return 0;
		
		return (new Date()).getTime() - date.getTime();
	}
	
	/**
	 * 
	 * 获取系统开机时间
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String getSTBStartupTime(){
		long longtime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Date dt = new Date(longtime);
		return format.format(dt);
	}
}
